# _*_ coding: utf-8 _*_
"""
Time:     2024/9/18
Author:   2051261
File:     task_model.py
"""
from schematics import Model
from schematics.types import StringType
from schematics.types import BooleanType
from schematics.types import ModelType
from schematics.types import DictType
from schematics.types import BaseType
from schematics.types import ListType
from schematics.exceptions import ValidationError

from extractor.flow.node_model.log_node import LogModel
from extractor.flow.node_model.schedule import ScheduleModel
from extractor.flow.node_model import handler_node_map


class HandlerNodeModel(Model):
    node_name = StringType(choices=handler_node_map.keys(), required=True)
    kwargs = DictType(BaseType, required=True)

    # 当调用validate, 会调用自己实现的所有以validate_开头的校验方法
    def validate_kwargs(self, node, kwargs):
        model = handler_node_map[node["node_name"]]
        try:
            model(kwargs).validate()
        except ValidationError as e:
            raise e


class TaskModel(Model):
    task_name = StringType(required=True)
    is_run = BooleanType(default=False)
    scheduler = ModelType(ScheduleModel, required=True)

    flow = ListType(ModelType(HandlerNodeModel), required=True)
    log = ModelType(LogModel, required=True)


if __name__ == "__main__":
    # 示例数据
    data = {
        "task_name": "dcv_web_c.web_szse_disciplinary_action",
        "is_run": True,
        "scheduler": {
            "trigger": "interval",
            "trigger_param": {
                "minutes": 2
            }
        },
        "flow": [
            {
                "node_name": "increment_where",
                "kwargs": {
                    "inc_column": "znz_ref",
                    "inc_type": "int",
                    "conn_name": "dcv_clean_db",
                    "table_name": "web_szse_disciplinary_action"
                }
            },
            {
                "node_name": "base_src_entity",
                "kwargs": {
                    "table_name": "web_szse_disciplinary_action",
                    "conn_name": "dcr_web_db",
                    "order_by": "znz_id",
                    "inc_column": "znz_id",
                    "operator": ">",
                    "strip_space": True
                }
            },
            {
                "node_name": "operate_columns",
                "kwargs": {
                    "modify_element": [
                        {
                            "operate_func": "simple_html_parse",
                            "kwargs": {
                                "parse_column": "ck",
                                "tag": "a",
                                "tag_column": "ck",
                                "element_key": "encode-open",
                            }
                        },
                        {
                            "operate_func": "add_str_prefix",
                            "kwargs": {
                                "str_prefix": "https://reportdocs.static.szse.cn",
                                "columns": ["ck"]
                            }
                        }
                    ],
                    "check_element": [
                        {
                            "operate_func": "is_not_empty_str",
                            "kwargs": {
                                "ref_column": "znz_ref",
                                "ref_from_column": "znz_id",
                                "columns": ["xx_fwrq"],
                                "mark_column": "znz_mark",
                                "mark_value": "2"
                            }
                        }
                    ]
                }
            },
            {
                "node_name": "base_dest_entity",
                "kwargs": {
                    "table_name": "web_szse_disciplinary_action",
                    "conn_name": "dcv_clean_db",
                    "dirty_conn_name": "dcv_dirty_db",
                    "version_conn_name": "dcv_clean_db",
                    "ver_pop_columns": ["znz_create_time", "znz_id"],
                    "pop_columns": ["znz_md", "znz_id", "znz_create_time"],
                    "ref_column": "znz_ref",
                    "ref_from_column": "znz_id",
                    "unique_key": ["xx_gsdm", "xx_fwrq", "xx_cflb", "xx_bt", "ck"],
                    "update_key": ["xx_gsdm", "xx_fwrq", "xx_cflb", "xx_bt", "ck"]
                }
            }
        ],
        "log": {
            "conn_name": "dc_log_db",
            "table_name": "dcv_web_log",
            "service_name": "dcr_web.web_szse_disciplinary_action",
            "task_name": "dcv_web_c.web_szse_disciplinary_action"
        }
    }

    # 创建 HandlerNodeModel 实例并验证
    handler_node = TaskModel(data)
    try:
        handler_node.validate()
        print("Validation succeeded.")
    except ValidationError as t:
        print(f"Validation error: {t}")
